package chelydra.sharelydra.client.webservice;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Hashtable;
import java.util.Map;

/**
 *
 * @author Pierre-Élie Fauché
 */
public class URLFactory {
	
	private String serverURL = "http://localhost:8080/ShareLydra/";
	private String userLogin;
	private String userPassword;

	// <editor-fold defaultstate="collapsed" desc="Getters/Setters">
	public String getServerURL() {
		return serverURL;
	}
	public void setServerURL(String baseURL) {
		this.serverURL = baseURL;
	}

	public void setUserLogin(String userLogin) {
		this.userLogin = userLogin;
	}

	public void setUserPassword(String userPassword) {
		this.userPassword = userPassword;
	}
	// </editor-fold>

	/**
	 * Returns the URL used to register a new user to the system.
	 * @param infos Map containing the information for the registration
	 * @return URL of the server
	 */
	protected String getURLForRegister(Map<String,String> infos) {
		return this.addParametersToURL(this.serverURL+"register?", infos);
	}

	/**
	 * Returns the URL to access a resource on the WebServer.
	 * The URL returned has only the login information supplied, no other parameters.
	 * @param actionName Name of the action on the server, used to construct the URL. See the URL patterns on server.
	 * @return
	 */
	protected String getURLForAction(String actionName) {
		return this.addLoginToURL(this.serverURL+actionName+"?");
	}

	/**
	 * Adds the parameters required for login on the WebServer to the given URL
	 * @param url
	 * @return
	 */
	protected String addLoginToURL(String url) {
		Map<String,String> parameters = new Hashtable(1);
		parameters.put("login", this.userLogin);
		parameters.put("password", this.userPassword);
		url = this.addParametersToURL(url, parameters);
		return url;
	}

	/**
	 * Adds a parameter to the given URL.
	 * @param url The basic URL
	 * @param parameterName Name of the new parameter
	 * @param parameterValue Value of the new parameter
	 * @return The basic URL with the new parameter
	 */
	protected String addParameterToURL(String url, String parameterName, String parameterValue) {
		Map<String,String> parameters = new Hashtable(1);
		parameters.put(parameterName, parameterValue);
		url = this.addParametersToURL(url, parameters);
		return url;
	}

	/**
	 * Add multiple parameters to the given URL.
	 * @param baseURL The basic URL
	 * @param parameters
	 * @return The basic URL with the new parameters
	 */
	protected String addParametersToURL(String baseURL, Map<String,String> parameters) {
		StringBuffer url = new StringBuffer(baseURL);
		String[] keys = parameters.keySet().toArray(new String[0]);
		String newParameter = null;
		String esperluette = "&";
		if (baseURL.endsWith("?"))
			esperluette = "";
		for (int i=0; i<keys.length; i++) {
			try {
				newParameter = new String(esperluette + URLEncoder.encode(keys[i], "UTF-8") +
																	"=" + URLEncoder.encode(parameters.get(keys[i]), "UTF-8"));
				url.append(newParameter);
				esperluette = "&";
			} catch (UnsupportedEncodingException ex) {
				ex.printStackTrace(System.err);
			}
		}
		baseURL = url.toString();
		return baseURL;
	}
}
